package cn.source.system.service.impl;

import cn.source.common.exception.base.BaseException;
import cn.source.common.utils.SecurityUtils;
import cn.source.config.IdConfig;
import cn.source.config.WxPayV3Config;
import cn.source.enums.PayTypeEnum;
import cn.source.system.domain.*;
import cn.source.system.domain.dto.VipPayDto;
import cn.source.system.service.*;
import cn.source.util.WxV3PayUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * @author tubo
 * @date 2024/06/13
 */
@Service
@Slf4j
public class PayServiceImpl implements PayService {

    @Resource
    private IdConfig idConfig;

    @Resource
    private WxPayV3Config wxPayV3Config;

    @Value("${local.address}")
    public String localAddress;

    @Resource
    IVipPriceService vipPriceService;

    @Resource
    private IVipOrderService vipOrderService;

    @Resource
    private IBuzMemberService buzMemberService;

    @Override
    public PayResultResponse vipPay(PayTypeEnum payType, VipPayDto vipPayDto) {
        String orderNo = idConfig.snowflakeId();
        //通过vip等级查出对应的vip信息
        //暂时写死 后续分其他类型

        //定义一个vipOrderModel
        VipOrderModel vipOrderModel = new VipOrderModel();
        vipOrderModel.setCost(new BigDecimal(vipPayDto.getVipPrice()));
        vipOrderModel.setOwner(vipPayDto.getUserId());
        vipOrderModel.setPayType(payType.getCode());
        vipOrderModel.setName(vipPayDto.getPriceClass());
        vipOrderModel.setVipId(vipPayDto.getVipId());
        //未支付
        vipOrderModel.setStatus(1);
        vipOrderModel.setOrderNo(orderNo);
        vipOrderModel.setCreatedAt(new Date());
        vipOrderModel.setUpdatedAt(new Date());

        vipOrderService.insertVipOrder(vipOrderModel);

        BuzMember buzMember = buzMemberService.selectBuzMemberById(vipPayDto.getUserId());
        PayResultResponse payResultResponse = new PayResultResponse();
        payResultResponse.setOrderNo(orderNo);
        payResultResponse.setPayType(payType.getCode());
        payResultResponse.setOrderId(String.valueOf(vipOrderModel.getId()));
        if (payType.equals(PayTypeEnum.WX_PAY)) {
            payResultResponse.setWxAppPayOrderInfo(WxV3PayUtil.createWxAppPayOrder(wxPayV3Config.getUserAppId(), vipOrderModel.getName(), orderNo,
                    this.localAddress + "/api/pay/wxPayVipNotifyUrl/" + payType.getCode(), new BigDecimal(vipPayDto.getVipPrice())));
            return payResultResponse;
        }
        if (payType.equals(PayTypeEnum.WX_JS_PAY)) {
            payResultResponse.setWxJsApiPayOrderInfo(WxV3PayUtil.createWxJsPayOrder(wxPayV3Config.getJsAppId(), vipOrderModel.getName(), orderNo,
                    this.localAddress + "/api/pay/wxPayVipNotifyUrl/" + payType.getCode(), new BigDecimal(vipPayDto.getVipPrice()), buzMember.getOpenid()));
        }
        return payResultResponse;
    }

    public PayResultResponse vipPay4web(PayTypeEnum payType, VipPayDto vipPayDto) {
        String orderNo = idConfig.snowflakeId();
        //通过vip等级查出对应的vip信息
        //暂时写死 后续分其他类型

        //定义一个vipOrderModel
        VipOrderModel vipOrderModel = new VipOrderModel();
        vipOrderModel.setCost(new BigDecimal(vipPayDto.getVipPrice()));
        vipOrderModel.setOwner(vipPayDto.getUserId());
        vipOrderModel.setPayType(payType.getCode());
        vipOrderModel.setName(vipPayDto.getPriceClass());
        vipOrderModel.setVipId(vipPayDto.getVipId());
        //未支付
        vipOrderModel.setStatus(1);
        vipOrderModel.setOrderNo(orderNo);
        vipOrderModel.setCreatedAt(new Date());
        vipOrderModel.setUpdatedAt(new Date());

        vipOrderService.insertVipOrder(vipOrderModel);

        BuzMember buzMember = buzMemberService.selectBuzMemberById(vipPayDto.getUserId());
        PayResultResponse payResultResponse = new PayResultResponse();
        payResultResponse.setOrderNo(orderNo);
        payResultResponse.setPayType(payType.getCode());
        payResultResponse.setOrderId(String.valueOf(vipOrderModel.getId()));
        if (payType.equals(PayTypeEnum.WX_PAY)) {
            payResultResponse.setWxAppPayOrderInfo(WxV3PayUtil.createWxAppPayOrder(wxPayV3Config.getUserAppId(), vipOrderModel.getName(), orderNo,
                    this.localAddress + "/api/pay/wxPayVipNotifyUrl/" + payType.getCode(), new BigDecimal(vipPayDto.getVipPrice())));
            return payResultResponse;
        }
        if (payType.equals(PayTypeEnum.WX_JS_PAY)) {
            payResultResponse.setWxJsApiPayOrderInfo(WxV3PayUtil.createWxJsPayOrder(wxPayV3Config.getJsAppId(), vipOrderModel.getName(), orderNo,
                    this.localAddress + "/api/pay/wxPayVipNotifyUrl/" + payType.getCode(), new BigDecimal(vipPayDto.getVipPrice())));
        }

        if (payType.equals(PayTypeEnum.WX_H5_PAY)) {
            String clientIP = vipPayDto.getClientIP();
            payResultResponse.setH5PayOrderInfo(WxV3PayUtil.createWxH5PayOrder(wxPayV3Config.getJsAppId(), vipOrderModel.getName(), orderNo,
                    this.localAddress + "/api/pay/wxPayVipNotifyUrl/" + payType.getCode(), new BigDecimal(vipPayDto.getVipPrice()),clientIP));
        }
        return payResultResponse;
    }
}
